// ignore_for_file: depend_on_referenced_packages

import 'dart:convert';
import 'package:aitao_flutter/chat/model/message_attachment_model.dart';
import 'package:aitao_flutter/common_ui/circle_avatar.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/utils/time_utils.dart';
import 'package:flutter/material.dart';
import 'package:nim_conversationkit/model/conversation_info.dart';
import 'package:nim_core/nim_core.dart';

class ConversationSystemItem extends StatefulWidget {
  const ConversationSystemItem({super.key, required this.info});
  final ConversationInfo info;
  @override
  State<ConversationSystemItem> createState() => _ConversationSystemItemState();
}

class _ConversationSystemItemState extends State<ConversationSystemItem> {
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
          border: Border(
              bottom: BorderSide(width: 1.0, color: ColorStyle.f8f8f8Color))),
      padding: const EdgeInsets.fromLTRB(0, 15, 0, 15),
      margin: const EdgeInsets.fromLTRB(15, 0, 15, 0),
      child: Row(children: [
        CircleAvatarWidget(url: widget.info.user?.avatar ?? '', gender: 4),
        Box.hBox10,
        Expanded(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.info.user?.nick ?? '系统通知',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: const TextStyle(fontSize: 15, color: ColorStyle.mainColor),
            ),
            Box.vBox5,
            Text(
              setLastMessageContent(),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style:
                  const TextStyle(fontSize: 14, color: ColorStyle.color777777),
            ),
          ],
        )),
        Box.hBox20,
        // const Spacer(),
        Text(
          TimeUtils.iMConversationshowTime(
              (widget.info.session.lastMessageTime ?? 0) ~/ 1000, false),
          style: const TextStyle(fontSize: 14, color: ColorStyle.color777777),
        ),
      ]),
    );
  }

  String setLastMessageContent() {
    String txt = '';
    switch (widget.info.session.lastMessageType) {
      case NIMMessageType.custom:
        Map<String, dynamic> attachment =
            widget.info.session.lastMessageAttachment!.toMap();
        debugPrint('attachment:$attachment');
        String obj = json.encode(attachment);
        debugPrint('obj:$obj');
        Map deObj = json.decode(obj);
        debugPrint('deObj:$deObj');
        AttachmentCommonModel? m =
            AttachmentCommonModel.fromJson(deObj['data']);
        return m.title ?? '';

      // if (deObj['type'] == 11) {
      //   // 礼物自定义消息
      //   txt = '一条礼物消息';
      // } else if (deObj['type'] == 14) {
      //   // 礼物自定义消息
      //   return '一条礼物消息';
      // }
      case NIMMessageType.text:
        txt = widget.info.session.lastMessageContent ?? '文本消息';
      case NIMMessageType.audio:
        txt = '[语音]';
      case NIMMessageType.image:
        txt = '[图片]';
      case NIMMessageType.video:
        txt = '[视频]';
      case NIMMessageType.location:
        txt = '[位置]';
      case NIMMessageType.notification:
        txt = '[通知类型]';
      case NIMMessageType.file:
        txt = '[文件类型]';
      case NIMMessageType.tip:
        txt = widget.info.session.lastMessageContent ?? '提醒消息';
      case NIMMessageType.robot:
        txt = '[机器人消息]';
      default:
        txt = '[未知消息]';
    }
    return txt;
  }
}
